package httpLogRequests;
import java.util.ArrayList;


public class Session {

	/** List of the request that occurred during the session*/
	private ArrayList<Request> requestList = new ArrayList<Request>();

	private boolean [] vectorPagesVisited;
	private ArrayList<String> namePages = new ArrayList<String>();

	public Session() {

	}

	/** 
	 * Adding a request to the list of request of the session
	 * @param req
	 *              the request to add
	 */
	public void addRequest(Request req) {
		int index = this.requestList.size();
		do {
			index --;
		}
		while (index >= 0 && req.getTimeStamp().getTime() < requestList.get(index).getTimeStamp().getTime());

		if (index == this.requestList.size() - 1) {
			this.requestList.add(req);
		}
		else {
			this.requestList.add(index + 1,req);
		}
	}

	/**
	 * Returns the most recent time a request was received
	 * @return
	 *              The value in seconds of the last time we received a request
	 */
	public long getLastTimeStamp() {
		return this.requestList.get(this.requestList.size() - 1).getTimeStamp().getTime();
	}


	/**
	 * Returns the number of requests of the user
	 * @return
	 *              the size of the ArrayList of request
	 */
	public int getNRequest() {
		return this.requestList.size();
	}

	/**
	 * IMPORTANT : this function must be run in order to build the boolean vector, that will be used
	 * by clustering algorithms
	 * @param pagesAvailable
	 *              The list containing the names of all the pages of the site
	 */
	public void buildBooleanVector(ArrayList<String> pagesAvailable) {
		this.vectorPagesVisited = new boolean[pagesAvailable.size()];
		for(Request req : this.requestList) {
			this.vectorPagesVisited[pagesAvailable.indexOf(req.getHttpRequest())] = true;
			this.namePages.add(req.getHttpRequest());
		}
	}


	public boolean[] getVectorPagesVisited() {return vectorPagesVisited;}

	public String toVectorPageVArrfString() {
		String toBeReturned = "";

		for(boolean b : this.vectorPagesVisited) {
			toBeReturned +=  (b? 1:0) +",";
		}


		return toBeReturned.substring(0,toBeReturned.lastIndexOf(","));
	}

	public ArrayList<Request> getRequestList() {return requestList;}
}